#include "MyGraphicsView.h"
#include "../shared/Bullet.h"
#include "../shared/Player.h"
#include "../shared/Map.h"



MyGraphicsView::MyGraphicsView(QWidget* parent) :
    QGraphicsView(parent)
{
    setRenderHint(QPainter::Antialiasing);
    setBackgroundBrush(QPixmap("../shared/map.jpg"));
    setCacheMode(QGraphicsView::CacheBackground);

    setScene(new QGraphicsScene);

    map = new Map(0, QPointF(0, 0));
    scene()->addItem(map);

    player = new Player(map, QPointF(120, 120), 1);
    player->setVisible(false);

    playersList = new QList<Player*>();

    Player *xplayer[3];
    for (int i = 0; i < 3; i++)
    {
        xplayer[i] = new Player(map, QPointF(120, 120+i), i+1);
        xplayer[i]->setVisible(false);
        playersList->push_back(xplayer[i]);
    }

    bulletsList = new QList<Bullet*>();

    Bullet *bullet[30];
    for (int i = 0; i < 30; i++)
    {
        if (i >= 15)
            bullet[i] = new Bullet(map, xplayer[1], QPointF(0, 0+i), QPointF(0, 0));
        else
            bullet[i] = new Bullet(map, xplayer[0], QPointF(0, 0+i), QPointF(0, 0));
        //bullet[i] = new Bullet(map, player, QPointF(0, 0+i), QPointF(0, 0));
        bullet[i]->setVisible(false);
        bulletsList->push_back(bullet[i]);
    }
    mouseX = mouseY = 0;
    show();
}


MyGraphicsView::~MyGraphicsView()
{
    delete map;
    delete player;
    playersList->clear();
    bulletsList->clear();
    delete playersList;
    delete bulletsList;
}



// obsluguje zdarzenia zwiazane z nacisnieciem myszy
void MyGraphicsView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        player->shoot = true;
        mouseX = event->x();
        mouseY = event->y();
    }
}


// obsluguje zdarzenia zwiazane z nacisnieciem klawisza
void MyGraphicsView::keyPressEvent(QKeyEvent *event)
{

    switch (event->key())
    {
    case Qt::Key_A:
        player->moveLeft = true;
        break;

    case Qt::Key_D:
        player->moveRight = true;
        break;

    case Qt::Key_W:
        player->moveUp = true;
        break;

    case Qt::Key_S:
        player->moveDown = true;
        break;
    }

}


// obsluguje zdarzenia zwiazane z puszczeniem klawisza
void MyGraphicsView::keyReleaseEvent(QKeyEvent *event)
{

    switch (event->key())
    {
    case Qt::Key_A:
        player->moveLeft = false;
        break;

    case Qt::Key_D:
        player->moveRight = false;
        break;

    case Qt::Key_W:
        player->moveUp = false;
        break;

    case Qt::Key_S:
        player->moveDown = false;
        break;
    }

}
